package com.mengchengnet.chaperonage.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.mengchengnet.chaperonage.BaseActivity;
import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.adapter.CommentAdapter;
import com.mengchengnet.chaperonage.domain.base.Comment;
import com.mengchengnet.chaperonage.util.PreferenceUtil;
import com.mengchengnet.chaperonage.util.Url;
import com.mengchengnet.chaperonage.view.refreshableListView.RefreshableListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/20.
 */

public class CommentActivity extends BaseActivity{
    private RefreshableListView listView;
    private List<Comment> list;
    private CommentAdapter adapter;
    private  RequestQueue requestQueue;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            adapter=new CommentAdapter(getApplicationContext(),list);
            listView.setPullLoadEnable(false);
            listView.setPullRefreshEnable(false);
            listView.setAdapter(adapter);
            //监听
            listView.setRefreshableListViewListener(new RefreshableListView.RefreshableListViewListener() {
                @Override
                public void onRefresh() {

                }

                @Override
                public void onLoadMore() {

                }
            });
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        toolbarState=TOOLBAR_STYLE_BACK_TITLE;
        initToolbar("用户评论");
        requestQueue= Volley.newRequestQueue(getApplication());
        listView= (RefreshableListView) findViewById(R.id.comment_listView);
        list = new ArrayList<>();
        //getData();
        get();
    }

    private void getData() {
        Comment c1=new Comment();
        c1.setContent("态度很好，很满意");
        c1.setHeadIcon("http://img3.imgtn.bdimg.com/it/u=1331343083,100664536&fm=26&gp=0.jpg");
        c1.setName("姜子牙");
        c1.setStar("3");
        c1.setTime("2016-05-30");

        Comment c2=new Comment();
        c2.setContent("还不错");
        c2.setHeadIcon("http://img3.imgtn.bdimg.com/it/u=1331343083,100664536&fm=26&gp=0.jpg");
        c2.setName("赵紫龙");
        c2.setStar("5");
        c2.setTime("2016-10-11");


        list.add(c1);
        list.add(c2);

        adapter=new CommentAdapter(this,list);
        listView.setPullLoadEnable(true);
        listView.setPullRefreshEnable(true);
        listView.setAdapter(adapter);
        //监听
        listView.setRefreshableListViewListener(new RefreshableListView.RefreshableListViewListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {

            }
        });

    }
    private void get(){
        StringRequest request = new StringRequest(Request.Method.POST, Url.getComment,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String result) {
                        Log.e("获取评论", "获取评论接口返回值："+result );
                        JSONObject jsonObject;
                        try {
                            if(result != null && result.startsWith("\ufeff"))
                            {
                                result =  result.substring(1);
                            }
                            jsonObject = new JSONObject(result);
                            if (jsonObject.getString("code").equals("00")){
                                JSONArray array=jsonObject.getJSONArray("db_list");
                                for (int i = 0; i <array.length() ; i++) {
                                    JSONObject obj=array.getJSONObject(i);
                                    String name=obj.getString("name");
                                    String time=obj.getString("time");
                                    String imgUrl=obj.getString("imgUrl");
                                    String level=obj.getString("level");
                                    String content=obj.getString("content");

                                    Comment c1=new Comment();
                                    c1.setContent(content);
                                    c1.setHeadIcon(imgUrl);
                                    c1.setName(name);
                                    c1.setStar(level);
                                    c1.setTime(time);
                                    list.add(c1);
                                }
                                handler.sendEmptyMessage(0);
                            }
                            else {
                                Toast.makeText(CommentActivity.this, "失败", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
                Toast.makeText(CommentActivity.this, "网络异常请稍后重试！", Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("version", PreferenceUtil.getString(getApplication(),"versionCode"));
                map.put("careId", getIntent().getStringExtra("careId"));
                return map;
            }
        };
        request.setTag("GetFenZu");
        request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, // 默认超时时间，应设置一个稍微大点儿的
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, // 默认最大尝试次数
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requestQueue.add(request);

    }

}
